package com.manor.common.http;

import com.manor.common.CommonAppConfig;
import com.manor.common.utils.DateUtil;
import com.manor.common.utils.DeviceUtil;
import com.manor.common.utils.Gson2Util;
import com.manor.common.utils.MD5Util;

import java.util.LinkedHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class JCommonHttpUtil {
    public static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
    public static final MediaType MULTIPART = MediaType.parse("multipart/form-data;charset=utf-8");

    /**
     * 取消网络请求
     */
    public static void cancel(String tag) {
        HttpClient.getInstance().cancel(tag);
    }


    public static String checkStr(){
        return CommonAppConfig.JAVA_HOST + "admin-sms/app/sms/getCaptcha?type=1&randomCode=";
    }



    /**
     * 用户首次认证()
     */
    public static void getZAY(String url, String baseStr, JHttpCallback callback) {
        String date = DateUtil.getNowString(DateUtil.getDateFormat("yyyyMMdd"));
        String u = CommonAppConfig.getInstance().getUid();
        String m = CommonAppConfig.SYSTEM_MODEL;

        String faceId = MD5Util.getMD5(date + HttpClient.S_JAVA + u + m);
        String zzId = MD5Util.getMD5(HttpClient.S_JAVA + date + u + url);
        String zxId = MD5Util.getMD5(u + HttpClient.S_JAVA + DeviceUtil.getOnlyID() + url + date);

        LinkedHashMap<String, Object> map = new LinkedHashMap();
        map.put("qiNiu", url);
        map.put("base64", "db");
        map.put("datetime", date);
        map.put("model", m);
        map.put("q", "rsp");
        map.put("defaultRandom", faceId);
        map.put("defaultRandom1", zzId);
        map.put("defaultRandom2", zxId);

        HttpClient.getInstance().jPost("admin-face/app/face/v1/face-distinguish", CommonHttpConsts.GET_FX_TH)
                .upRequestBody(PitUtil.getBody(map))
                .execute(callback);
    }





    /**
     * 用户提现人脸检测
     * type 类型 1.扫脸支付 2.登录扫脸
     */
    public static void extractAuth(String type, String baseStr, JHttpCallback callback) {
        LinkedHashMap<String, Object> map = new LinkedHashMap();
        map.put("type", type);
        map.put("base64", baseStr);
        map.put("devCode", DeviceUtil.getOnlyID());

        RequestBody requestBody = RequestBody.create(JSON, Gson2Util.toJson(map));
        HttpClient.getInstance().Jpost("admin-face/app/face/v1/search-face", CommonHttpConsts.SEARCH_FACE)
                .upRequestBody(PitUtil.getBody(map))
                .execute(callback);
    }


    /**
     * 随机金额和付费
     *
     * @param type 1付费，0获取
     */
    public static void getNeedOrPay(String type, JHttpCallback callback) {
        LinkedHashMap<String, Object> map = new LinkedHashMap();
        map.put("type", type);

        RequestBody requestBody = RequestBody.create(JSON, Gson2Util.toJson(map));
        HttpClient.getInstance().Jpost("admin-transfer/app/face/v1/random-price", CommonHttpConsts.PAY_AUTH_MONEY)
                .upRequestBody(PitUtil.getBody(map))
                .execute(callback);
    }



    /**
     * 获取蜜糖做任务(视频看广告)
     * 任务类型(1广告 2发动态)
     */
    public static void getAdTask(String id,String vCode,String timeStamp,String random,String imgCode,String type,
                                 JHttpCallback callback) {
        String faceId = MD5Util.getMD5(DeviceUtil.getOnlyID()+ CommonAppConfig.getInstance().getUid()+ HttpClient.S_JAVA);

        LinkedHashMap<String, Object> map = new LinkedHashMap();
        map.put("defaultRandom", faceId);
        map.put("type", 1);
        map.put("taskId", id);
        map.put("verifyCode", vCode);
        map.put("timeStamp", timeStamp);

        map.put("randomCode", random);
        map.put("inpCode", imgCode);

        //doTaskSourceType：任务类型(1.短视频页面,2.任务页面)
        map.put("doTaskSourceType", type);

        HttpClient.getInstance().Jpost("admin-dotask/app/spirit/v1/finish-task", CommonHttpConsts.DO_AD_TASK)
                .upRequestBody(PitUtil.getBody(map))
                .execute(callback);
    }



    public static void doTaskIsFinish(String type, JHttpCallback callback) {
        LinkedHashMap<String, Object> map = new LinkedHashMap();
        map.put("doTaskSourceType", type);

        HttpClient.getInstance().Jpost("admin-dotask/app/spirit/v1/today/is-finish-task",
                CommonHttpConsts.DO_TASK_IS_FINISH)
                .upRequestBody(PitUtil.getBody(map))
                .execute(callback);
    }


}
